package com.classic.okhttp.base.request;

import com.classic.okhttp.base.consts.OkHttpConst;
import com.classic.okhttp.base.utils.Exceptions;
import java.io.File;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by zhy on 15/12/14.
 */
public class PostFileRequest extends OkHttpRequest {

    private File file;
    private MediaType mediaType;

    public PostFileRequest(String url, Object tag, Map<String, String> params,
        Map<String, String> headers, File file, MediaType mediaType) {
        super(url, tag, params, headers);
        this.file = file;
        this.mediaType = mediaType;

        if (this.file == null) {
            Exceptions.illegalArgument("the file can not be null !");
        }
        if (this.mediaType == null) {
            this.mediaType = OkHttpConst.MEDIA_TYPE_STREAM;
        }
    }

    @Override protected RequestBody buildRequestBody() {
        return RequestBody.create(mediaType, file);
    }

    @Override protected Request buildRequest(Request.Builder builder, RequestBody requestBody) {
        return builder.post(requestBody).build();
    }

    @Override public String toString() {
        return super.toString() + ", requestBody{ uploadfilePath = " + file.getAbsolutePath() + " } ";
    }
}
